<template>
  <ych-modal
    :fullscreen="false"
    :title="title"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    :cancelText="$t('m.common.button.close')">
    <view-select-redirect ref="container" :viewCode="viewCode" :multi="false" :rows="rows" :keys="keys"/>
  </ych-modal>
</template>

<script>

  // DEMO 页面

  import ViewSelectRedirect from '../viewSelect/ViewSelectRedirect'
  import { putAction } from '@/api/manage'

  export default {
    name: 'ViewListSelectDialog',
    components: {
      ViewSelectRedirect,
    },
    props: {
      obj: Object
    },
    data () {
      return {
        viewCode: 'ych_demo',
        confirmLoading: false,
        title: 'demo',
        visible: false,
        rows: [],
        keys: [],
        url: {
          update: 'xx'
        }
      }
    },
    created () {
      this.visible = true
    },
    methods: {
      handleOk () {
        if (this.$refs.container.rowSelection.selectionRows.length == 0) {
          this.messageUtil.warning(this.$t('m.eform.formDesign.label.qing-xuan-ze-shu-ju'))
          return
        }
        this.selectFinished(this.$refs.container.rowSelection.selectionRows[0].id)
      },
      selectFinished (id) {
        this.confirmLoading = true
        putAction(this.url.update + '?id=' + id, {}).then((res) => {
          if (res.success) {
            this.$emit('refreshView', 'ych_demo_list');
            this.messageUtil.success(res.message)
            this.handleCancel()
          } else {
            this.messageUtil.warning(res.message)
          }
        }).catch((e) => {
          console.error(e)
          this.messageUtil.error(this.$t('m.common.tip.operate_error'))
        }).finally(() => {
          this.confirmLoading = false
        })
      },
      handleCancel () {
        this.visible = false
      },
    },
  }
</script>
